Teaching Geometry with Liberty BASIC © - novice level

MEASURING AN ANGLE

by Janet Terra

Home

Tips For the Hobbyist

Nally's Applications

Measuring an Angle

Maphandle Listboxes

About GUIs

Convert Old Code

Installing Fonts

Tip Corner

Stopwatch for LB4

Run with Params

Newsletter help

Index


Introduction

Walk into any classroom, from kindergarten to university, and you're likely to see some type of LCD projector, perhaps even a 'SmartBoard ©,' in use. Just as likely, the images being displayed are derived from a PowerPoint © slide presentation. How unfortunate that such a sophisticated tool is being used as little more than an expensive overhead projector. Consider teaching geometry, specifically using a protractor to measure angles, to sixth - graders with PowerPoint ©. My guess is, there would first be one or more terminology / definitions slides, followed by a number of predetermined/predrawn angles; in fact, the slides would be derived from last year's transparencies. The sheer number of slides is costly in terms of developer's time, disk storage, and memory usage. Liberty BASIC ©, on the other hand, provides a way to produce limitless, random angles in very little time, requiring very little storage and costing minimal memory.

The following code presents a display of randomly determined angles with the option of measuring that angle using a protractor. This is a very simple program, containing only native Liberty BASIC © commands. The main purpose of this article is to offer one example of how a program programmed in Liberty BASIC © can be a valuable teaching tool.

For newbies, like myself, forum discussions introduce new commands. For seasoned programmers, these discussions may bring to mind seldom used commands. Such was the case last month when a text graphics formatting query was posed. A second discussion focused on using graphics memory clearing commands effectively. This article serves also to (1) illustrate how the stringwidth? command, coupled with the posxy xVar yVar command, can be used to format non-linear displays and (2) provide a better understanding of how the flush - discard - delsegment commands are used to conserve memory.

The Screens

There are only two screens in this program: the title screen and the angle screen. Once the GUI controls are defined, and the window is opened, the title screen is displayed. The discussion that follows assumes you have copied the code and are following along. It should also be mentioned that any line beginning with print #handle, "command" can be abbreviated to #handle, "command". Thus,

print #w.g, "\Title"

is the same as

#w.g, "\Title"



[titleScreen]

The title screen is just barebones. Notice the use of the stringwidth? command in centering the text. Can the text be centered without using the stringwidth? command? Absolutely. But, if the font style or font size is changed, so, too, must the centering code. Change the line

#w.g, "font times_new_roman 44"
to
#w.g, "font times_new_roman 72"

to see stringwidth? in action. Without this command, the len(t$) would need to be multiplied by the average number of pixels in each font letter, or perhaps another no less elaborate scheme.

After the title screen is drawn, the commands

#w.g, "segment drawSegment"
#w.g, "delsegment ";drawSegment -1
#w.g, "flush"

are issued to (1) get the segment number of the last draw set of commands ("segment drawSegment"), (2) delete the prior set of commands from memory ("delsegment ";drawSegment -1), and (3) commit the most recent set of instructions to memory for a possible redraw later ("flush").

The helpfile included with Liberty BASIC © gives some information regarding delsegment and flush, but a better explanation is provided in Alyce Watson's Mastering Liberty BASIC 3 ©. This same information can also be found in the Liberty BASIC Newsletter #102 (see SEGMENTS AND FLUSHING - novice level by Alyce Watson).

angle

[angleScreen]

This is the code that randomly assigns the angle. Since the protractor is measured in increments of 2 (degrees), the angles should also be even. Remember that the intended audience for this program is a classroom of sixth - graders. There's no sense providing an example whose measurement is debatable. Hence, using the commands

a = int(rnd(1)*90)+1
angle = 2*a

ensures an even measurement from 2 to 180.

As with the titleScreen, the angleScreen concludes with the commands

#w.g, "segment drawSegment"
#w.g, "delsegment ";drawSegment -1
#w.g, "flush"

to free up memory otherwise busy with remembering past drawing details and to then store in memory the current drawing instructions. Later, this will be seen more concretely and vividly.

The Beginning of an Angle

The aforementioned Alyce Watson wrote an excellent article detailing the uses and differences between subs and subroutines (see Subroutines and Gosubs, Liberty BASIC Newsletter #114). Suffice it to say here that

sub leg angle

is used twice to draw the determined angle. The first leg sloped at the angle of 0 and the second leg sloped at the computer generated angle (even, 2 - 180).

In programming, angles can be a bit exasperating. Turtle logo graphics use the command north to point the turtle in the up position. One would think, then, that 0 degrees is the angle perpendicular to the horizontal line. Not so. Draw a pie from angle 0 to angle 180 and you'll find the pie starts due east (angle 0) and ends due west (angle 180). So, 0 degrees is really east and not north. Of course, there's no east, south, or west turtle graphics command.

Not only is 0 not north, but a protractor placed on a horizontal line would place 0 at due west. That's north plus 270 degrees, or north minus 90 degrees, or angle 180. Someone with a lot more expertise than me could explain why. Suffice it to say, I don't understand it, but I accept it and adjust.

This adjustment lies first in sub leg angle. The command

#w.g, "north; turn -90"

resets the direction to due north, then turns -90 degrees counterclockwise to due west. And due west is exactly where we humans like to start reading a protractor placed on a horizontal line. This adjustment will show up again in the code that draws the protractor. Notice that the sub does not contain the commands segment, delsegment, and flush, because the screen display isn't yet complete. To do so would cause a computer memorization of a partial screen display.

[showProt]

It's often said that programming is both a science and an art. If [angleScreen] is the science of this program, then [showProt] is the art. And art is much more difficult than science. The adjustment of beginning due west, then progressing by degrees clockwise, is used once again here. A for - next loop defines the angles. Up, down, and go statements draw the measuring lines. Color is used to help clarify the 10, 20, 30, ... 180 lines. Remember that the sixth - grader sitting in the back row has to see as clearly as the child in the front row.

The measuring lines need to be numbered as well. This is an ideal use of the commands

#w.g, "posxy ";x;" ";y
#w.g, "stringwidth? a$ width"
#w.g, "place ";x-width/2;" ";y+3

These commands are used to (1) determine where the pen has reached, (2), get the width of the current number converted to a string, and (3) center that number on the sloped line. All this is done without the programmer knowing (or caring) what the x and y coordinates are.

The protractor is then given a finishing outline for a more professional, less homemade look. This part is a bit perplexing. One would expect that a pie command, once the angle adjustments were made, would take care of this simply and efficiently. Not so. I couldn't seem to draw a perfect hemisphere using the pie command. This may have something to do with pen sizes. Again, I resorted to my philosophy of "When you can't understand something, accept it and adjust." Originally, I settled for finishing the protractor using first a circle command, and then the boxfilled command to paint over (erase) the lower half. Then, as usual, I found a better way when perusing the newsletters. I'm not even going to pretend to understand the code that draws the arc, but it works. Thanks to David Drake and Tom Nally. (See Four Methods for Drawing Arcs, Liberty BASIC Newsletter #109.)

As with the sub leg angle routine, there are no segment, delsegment, flush commands following the [showProt] code. Dragging the window beyond the monitor screen, maximizing, minimizing, etc. will lose the drawn protractor because the protractor was not 'flushed' into memory. The angleScreen (or titleScreen) persists, though, because each was flushed. Instead, the [showProt] block ends with the discard command. This releases memory which would otherwise be uselessly consumed saving the drawing instructions needed for the protractor. No need. The protractor directions can easily be given again should the user choose.

[hideProt]

There are several viable options for removing the protractor drawing from the angleScreen. One would be to erase the angleScreen with a cls command and then redraw the angle at the same measurement. Those instructions were flushed into memory, though. The directions for drawing the protractor were discarded. Only one command is needed to reissue the drawing commands. That command is

#w.g, "redraw"

Simple and efficient.

Menus and HotKeys

The program contains some simple menus and hotkeys. The hotkeys are listed in the Help notice. My thanks to Chris Lukas for showing me how to format notices, confirms, and prompts using chr$(13). The commands menu, "when characterInput", and "setfocus" are pretty well documented in the helpfile. One word of caution, though. Liberty BASIC doesn't seem to mind if menu is typed Menu, or setfocus is typed SetFocus, but it's pretty particular that characterInput is spelled/capitalized EXACTLY that way.

Seeing Delsegment in Action

This article has thus far focused on using the delsegment and flush command. Rem out (place an apostrophe or the phrase rem prior to the command) any flush command and maximize/minimize the display (or drag it beyond the monitor limits) to see how flush conserves the drawing. To see delsegment in action, rem out the commands

#w.g, "segment drawSegment"
#w.g, "delsegment ";drawSegment -1

in both the [titleScreen] code and the [angleScreen] code. Use the hotkey n, for new angle, and keep it depressed for some time. Alternate between depressing the n and the t key for several seconds each. Finish in the angleScreen view. Show the protractor, then hide the protractor. Hiding the protractor will give lightning fast glimpses of previous anglesScreens / titleScreens before settling on the last angleScreen. This is the computer 'redrawing' all those undeleted commands. (Note: this strobe effect should be avoided if you have a history of seizure activity.) Now go back and restore the commands by removing the rem or apostrophe. Perform this same test again. The delsegment commands have removed those unnecessary (and annoying) graphic commands from memory. Look at all the memory you've reclaimed for more important things (like defining a function that adjusts for those due east angles).

Conclusion

This is just one simple example of how a BASIC program can be used in the classroom. Liberty BASIC © is fairly easy to learn and the resources are abundant and economically attractive. With an endless supply of computer generated random scenarios, defined by you, the teacher, combined with the capability of student interaction, Liberty BASIC © programs can be powerful adjuncts to education at any level. A static PowerPoint © slide presentation or a dynamic Liberty BASIC © program: Which would you choose?




''''''''''''''''''''''''''''''''''''
' Measuring an Angle               '
' by Janet Terra                   '
' janetterra@yahoo.com             '
''''''''''''''''''''''''''''''''''''
' Made with Liberty BASIC v3.03    '
'    by Carl Gundel                '
''''''''''''''''''''''''''''''''''''


'measureangle.bas
    'nomainwin


'Define the Dimensions of the Window
    WindowWidth = 800
    WindowHeight = 600


'Define the Menus
    Menu #w, "Options","Title Screen",[titleScreen], _
        "New Angle",[angleScreen],|,"End Program",[endProgram]
    menu #w, "Protractor","Show Protractor",[showProt], _
        "Hide Protractor", [hideProt]
    menu #w, "Help","Help",[help], "About",[about]


'Open a Graphic Box in the Program's Main Window
    graphicbox #w.g, 5,5,785,540
    open "Measuring an Angle" for window as #w


'SetTrapclose
    #w, "trapclose [endProgram]"


'Trap HotKeys
    #w.g, "When characterInput [hotKeys]"
    #w.g, "SetFocus"


'Initialize Variables
    pi = 3.14159265358979


    [titleScreen]
    #w.g, "down"
    #w.g, "fill darkcyan"
    #w.g, "up"
    #w.g, "color black"
    #w.g, "backcolor darkcyan"
    t$ = "Measuring an Angle"
    #w.g, "font times_new_roman 44"
    #w.g, "stringwidth? t$ width"
    #w.g, "place ";392-width/2;" 250"
    #w.g, "\";t$
    t$ = "Using Liberty BASIC Programming Language"
    #w.g, "font times_new_roman 18"
    #w.g, "stringwidth? t$ width"
    #w.g, "place ";392-width/2;" 300"
    #w.g, "\";t$
    #w.g, "segment drawSegment"
    #w.g, "delsegment ";drawSegment -1
    #w.g, "flush"
wait


    [angleScreen]
    #w.g, "fill darkcyan"
    #w.g, "color black"
    #w.g, "size 3"
    angle = 0
    call leg angle
    a = int(rnd(1)*90)+1
    angle = 2*a
    call leg angle
    #w.g, "font times_new_roman 14 bold"
    #w.g, "place 50 50"
    #w.g, "\Angle = ";angle;" degrees"
    #w.g, "segment drawSegment"
    #w.g, "delsegment "; drawSegment -1
    #w.g, "flush"
wait


    sub leg angle
    #w.g, "place 400 400"
    #w.g, "north; turn -90"
    #w.g, "turn ";angle
    #w.g, "down"
    #w.g, "go 375"
    end sub


    [showProt]
    #w.g, "size 3"
    for a = 0 to 180 step 2
    #w.g, "color darkblue"
    #w.g, "up"
    #w.g, "place 400 400"
    #w.g, "north; turn -90"
    #w.g, "turn ";a
    if a/10 = int(a/10) then
        #w.g, "color brown"
        #w.g, "down"
    end if
        #w.g, "go 150"
    #w.g, "down"
    #w.g, "go 50"
    next a
    #w.g, "font times_new_roman 8 bold"
    #w.g, "color black"
    #w.g, "up"
    for a = 10 to 170 step 10
    a$ = str$(a)
    #w.g, "stringwidth? a$ width"
    #w.g, "place 400 400"
    #w.g, "north; turn -90"
    #w.g, "turn ";a
    #w.g, "go 150"
    #w.g, "posxy x y"
    #w.g, "place ";x-width/2;" ";y+3
    #w.g, "\";a$
    next a
    #w.g, "size 4"
    #w.g, "color darkred"
    #w.g, "place 400 400"
    #w.g, "down"
    for angle = 3.14 to 6.28 step .02
    x = 400 + 200*cos(angle)
    y = 400 + 200*sin(angle)
    #w.g, "goto ";x;" ";y
    next angle
    #w.g, "goto 400 400"
    #w.g, "up"
wait


    [hideProt]
    #w.g, "redraw"
wait


    [help]
    help$ = "Measuring Angles"+chr$(13)
    help$ = help$;"To display a random angle"+chr$(13)
    help$ = help$;"    From Options, choose New Angle"+chr$(13)+chr$(13)
    help$ = help$;"To measure that angle"+chr$(13)
    help$ = help$;"    From Protractor, choose Show Protractor"+chr$(13)+chr$(13)
    help$ = help$;"Hotkeys"+chr$(13)
    help$ = help$;space$(7);"t";space$(6);"=  Title Screen"+chr$(13)
    help$ = help$;space$(7);"n";space$(6);"=  New Angle"+chr$(13)
    help$ = help$;space$(7);"s";space$(6);"=  Show Protractor"+chr$(13)
    help$ = help$;space$(7);"h";space$(6);"=  Hide Protractor"+chr$(13)
    help$ = help$;space$(3);" =  End Program"+chr$(13)
    notice help$
wait


    [about]
    about$ = "Measuring Angles"+chr$(13)
    about$ = about$;"Written in Liberty BASIC v3.03"+chr$(13)
    about$ = about$;"Tested in Liberty BASIC v4.0"+chr$(13)
    about$ = about$;"Submitted to Liberty BASIC Newsletter"+chr$(13)
    about$ = about$;"for January, 2004, inclusion";chr$(13)
    about$ = about$;"Released as open code";chr$(13)+chr$(13)
    about$ = about$;"Liberty BASIC authored by Carl Gundel"+chr$(13)
    about$ = about$;"www.libertybasic.com"+chr$(13)
    notice about$
wait


    [hotKeys]
    key$ = Inkey$
    if key$ = "t" then [titleScreen]
    if key$ = "n" then [angleScreen]
    if key$ = "s" then [showProt]
    if key$ = "h" then [hideProt]
    if asc(key$) = 27 then [endProgram]
wait


    [endProgram]
    confirm "End Program?";ans$
    if ans$ <> "yes" then
        wait
    end if
    close #w
end


Home

Tips For the Hobbyist

Nally's Applications

Measuring an Angle

Maphandle Listboxes

About GUIs

Convert Old Code

Installing Fonts

Tip Corner

Stopwatch for LB4

Run with Params

Newsletter help

Index